package rbac.View.Main.Service.array;

import rbac.View.Main.DoMain.Dto.DeptDto;
import rbac.View.Main.DoMain.Entity.Dept;
import rbac.View.Main.DoMain.Vo.DeptVo;
import rbac.View.Main.DoMain.Vo.Result;
import rbac.View.Main.Service.IDeptService;
import rbac.View.Main.Service.IUserService;

public class DeptService extends  BaseService<Dept,DeptDto, DeptVo> implements IDeptService {

    private IUserService userservice;

    private static  IDeptService singleton;

    private DeptService(){
        data=new Dept[DATA_SIZE+1];
        temp=new DeptVo[DATA_SIZE+1];
    }
    public static IDeptService getInstance(){
        if(singleton==null){
            singleton=new DeptService();
        }
        return singleton;
    }

    @Override
    DeptVo convertToVo(Dept dept) {
        return new DeptVo(dept.getId(),dept.name,dept.phone);
    }
    @Override
    Dept createEntity(DeptDto dto,int id){
        return new Dept(id,dto.name,dto.phone);
    }

    @Override
    void updateEntity(DeptDto dto,Dept dept){
        dept.name=dto.name;
        dept.phone=dto.phone;
    }

    @Override
    public  Result delete(int id){
        if(existsById(id)){
            return Result.faliure("删除失败，不存在该数据");
        }
        if(userservice==null){
            userservice= UserService.getInstance();
        }
        if(userservice.deptInUse(id)){
            return Result.faliure("删除失败，还有用户属于该部门，不能删除");
        }
        data[id]=null;
        return Result .success("删除成功",null);
    }
}

